<template>


  <el-dialog
    top="20px"
    :title="defaultValue.title"
    :lock-scroll="tableConfig.lockScroll"
    :append-to-body="tableConfig.appendToBody"
    :visible="tableConfig.show"
    @close="tableConfig.show = false"
    :show-close="tableConfig.close"
    :destroy-on-close="tableConfig.destroyOnClose"
    width="1100px">

    <el-form size="small" ref="formOpsDeployRef" :rules="checkRule" :model="submitData" label-width="180px">

        <el-form-item label="场所" >
            <el-input disabled :value="site ? site.name : ''" />
        </el-form-item>
        <el-form-item label="计算机" >
            <el-input disabled :value="computerServerVo ? computerServerVo.name : ''" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input disabled value="应用" />
        </el-form-item>

        <el-form-item label="所属产品" prop="productName">
          <el-input v-model="deployInfo.opsDeploy.productName" disabled />
        </el-form-item>

        <el-form-item label="所属组件" prop="productName">
          <el-input v-model="deployInfo.name" disabled />
        </el-form-item>

        <el-form-item label="所属组件版本" prop="productName">
          <el-input v-model="deployInfo.opsComponentVersion.version" disabled />
        </el-form-item>



        <el-form-item label="环境" prop="envId">
          <el-select v-model="submitData.envId" placeholder="请选择环境" clearable @change="envChangeHandle">
            <el-option
              v-for="item in envOpt"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>


    </el-form>


    <span slot="footer" class="dialog-footer">
      <el-button plain type="warning" @click="toSaveUpd"
                 v-if="defaultValue.saveUpdSelect !== 'select'">{{ defaultValue.title }}
                 </el-button>
      <el-button plain type="primary" @click="tableConfig.show = false">关闭</el-button>
    </span>

  </el-dialog>


</template>

<script>

import http from '@/utils/http'
import {clearInvalid} from "@/utils/utils"

export default {
  name: "OpsDeployComponent",
  data() {
    return {
      // 表格的默认值
      tableConfig: {
        // 关闭时销毁 Dialog 中的元素
        destroyOnClose: true,
        // 弹出框是否打开
        show: false,
        // 是否显示关闭按钮
        close: true,
        // 下面2个属性解决打开弹出框后没有在最上面显示的问题，导致看到的内容是一半的，需要先将滚动条移到最上面
        lockScroll: false,
        appendToBody: true,
      },
      // 默认值，进入页面后的值
      defaultValue: {
        // 弹出框标题
        title: '查看',
        // 保存还是修改or查看,取值有 select | save | upd
        saveUpdSelect: 'select'
      },
      // 提交的数据
      submitData:{
        id: null,
        opsDeployId: null,
        envId: null,
        productId: null,
      },
      // 校验规则
      checkRule: {
        envId: [{required: true, message: '请选择环境', trigger: 'change'}],
      },
      // 场所
      site: null,
      // 当前计算机
      computerServerVo: {},
      // 环境信息
      envOpt: [],
      // 组件信息
      componentOpt: [],
      // 组件版本
      componentVersionOpt: [],
      // 部署信息
      deployInfo: {
        opsDeploy: {},
        opsComponentVersion: {},
      },
    }
  },
  methods: {
    /**
     * 初始化操作
     * @param operateType 操作类型,保存还是修改or查看,取值有 select \ save \ upd
     * @param deployInfo 组件和部署信息
     * @param computerServerId 服务器id
     */
    init(operateType, deployInfo, computerServerId){
      this.defaultValue.saveUpdSelect = operateType
      this.deployInfo = deployInfo
      this.clearOldData()
      this.getSiteById()
      this.getEnvListPage()
      this.getComputerServerDetails(computerServerId)
      this.submitData.opsDeployId = this.deployInfo.deployId

      if (operateType === 'save'){
        this.defaultValue.title = '新增'

      } else if (operateType === 'upd'){
        this.defaultValue.title = '修改'
        // 要json处理下，否则会影响之前的值
        // this.submitData = JSON.parse(JSON.stringify(obj))

      } else {
        // 可能查看
      }

      this.tableConfig.show = true
    },
    /**
     * 清理掉旧数据
     */
    clearOldData(){
      this.submitData = {
        id: null,
        opsDeployId: null,
        envId: null,
        productId: null,
      }
    },
    /**
     * 是保存还是修改
     */
    toSaveUpd(){
      if (this.defaultValue.saveUpdSelect === 'save'){
        this.saveOpsDeploy()

      } else if (this.defaultValue.saveUpdSelect === 'upd'){
        this.updOpsDeploy()

      } else {

      }
    },
    /**
     * 保存
     */
    saveOpsDeploy(){
      http.post("/opsDeployLink/saveOpsDeployLink", this.submitData).then(response => {
        let res = response.data;

        this.$message.success ("保存成功");
        this.$emit('getListPage', null)
        // 关闭弹出框,继续添加
        this.tableConfig.show = false

      }).catch(error => {
        this.$message.error (error.message);
      })
    },
    /**
     * 根据主键查询场所信息
     */
    getSiteById() {
      http.post("/opsSite/getSiteById", {
        siteId: this.$route.query.siteId,
      }).then(response => {
        let res = response.data;
        this.site = res.data

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
    /**
     * 计算机详情
     * @param computerServerId 服务器id
     */
    getComputerServerDetails(computerServerId) {
      http.post("/computer/getComputerServerDetails", {
        computerServerId: computerServerId
      }).then(response => {
        let res = response.data;
        this.computerServerVo = res.data

      }).catch(error => {
        this.$message.error(error.message);
      })
    },
    /**
     * 环境信息查询
     */
    getEnvListPage() {
      let query = {
        current: 1,
        size: 1000,
        siteId: this.$route.query.siteId,
        currentDeployId: this.deployInfo.deployId,
      }
      http.post("/opsEnv/getReuse", query).then(response => {
        let res = response.data;
        this.envOpt = res.data;

      }).catch(error => {
        this.$message.error (error.message);
      })
    },
    /**
     * 环境的值发生变化
     * @param value 目前的选中值
     */
    envChangeHandle(value){
      // 根据 id 找到完整 item,环境信息
      const item = this.envOpt.find(i => i.id === value) || null;
      // 获取应用信息
      this.getComponentByProductId(item)
    },
    /**
     * 根据产品id查询应用
     * @param env 环境信息
     */
    getComponentByProductId(env){
      http.post("/opsComponent/getComponentByProductId", {
        productId: env.productId
      }).then(response => {
        let res = response.data;
        this.componentOpt = res.data;

      }).catch(error => {
        this.$message.error (error.message);
      })
    },
    /**
     * 组件的值发生变化
     * @param value 组件id
     */
    componentChangeHandle(value){
      let query = {
        current: 1,
        size: 999,
        componentId: value
      }
      http.post("/opsComponentVersion/getOpsComponentVersionPageList", query).then(response => {
        let res = response.data;
        // 分页数据
        let data = res.data;
        // this.pageConfig.total = data.total;
        // this.pageConfig.current = data.current;
        // this.pageConfig.size = data.size;
        // 表格数据
        this.componentVersionOpt = data.records

      }).catch(error => {
        this.$message.error(error.message);
      })
    },

  }
}
</script>

<style scoped>

</style>


